<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/murmur/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/murmur/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/murmur/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/murmur/images/logo.svg" color="#222">

<link rel="stylesheet" href="/murmur/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.1/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.css">
  <script src="//cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.js"></script>

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"gitee.com","root":"/murmur/","scheme":"Gemini","version":"8.0.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"disqus","storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}};
  </script>

  <meta name="description" content="理解数组的概念，使用一维和多维数组；理解字符串概念，认识不可变的String类以及可变的StringBuffer类；理解向量的概念，掌握操作向量的相关方法；">
<meta property="og:type" content="article">
<meta property="og:title" content="计应本的自考笔记(5)-Java数组、字符串与向量">
<meta property="og:url" content="https://gitee.com/lianghuilin/murmur.git/university_notes/Java(5)-%E6%95%B0%E7%BB%84%E3%80%81%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%8E%E5%90%91%E9%87%8F/index.html">
<meta property="og:site_name" content="汇话板">
<meta property="og:description" content="理解数组的概念，使用一维和多维数组；理解字符串概念，认识不可变的String类以及可变的StringBuffer类；理解向量的概念，掌握操作向量的相关方法；">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-03-26T14:10:12.000Z">
<meta property="article:modified_time" content="2022-03-27T08:48:08.498Z">
<meta property="article:author" content="William Leung">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://gitee.com/lianghuilin/murmur.git/university_notes/Java(5)-%E6%95%B0%E7%BB%84%E3%80%81%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%8E%E5%90%91%E9%87%8F/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>计应本的自考笔记(5)-Java数组、字符串与向量 | 汇话板</title>
  


  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?a49081c570a782a242cdf6e19e1a584d";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/murmur/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">汇话板</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">设定目标，放慢步调，用温和的力量自我改革</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/murmur/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/murmur/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <section class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%80%E7%BB%B4%E6%95%B0%E7%BB%84"><span class="nav-number">1.</span> <span class="nav-text">一维数组</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%A4%9A%E7%BB%B4%E6%95%B0%E7%BB%84"><span class="nav-number">2.</span> <span class="nav-text">多维数组</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%B1%BB%E5%9E%8B"><span class="nav-number">3.</span> <span class="nav-text">字符串类型</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#String%E4%B8%8EStringBuffer"><span class="nav-number">3.1.</span> <span class="nav-text">String与StringBuffer</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%93%8D%E4%BD%9C%E4%B8%8E%E5%88%A4%E6%96%AD"><span class="nav-number">3.2.</span> <span class="nav-text">字符串操作与判断</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#String%E5%92%8CStringBuffer%E5%85%B1%E6%9C%89%E6%96%B9%E6%B3%95"><span class="nav-number">3.2.1.</span> <span class="nav-text">String和StringBuffer共有方法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#String%E7%B1%BB%E7%8B%AC%E6%9C%89%E7%9A%84%E6%96%B9%E6%B3%95"><span class="nav-number">3.2.2.</span> <span class="nav-text">String类独有的方法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#StringBuffer%E7%B1%BB%E7%8B%AC%E6%9C%89%E7%9A%84%E6%96%B9%E6%B3%95"><span class="nav-number">3.2.3.</span> <span class="nav-text">StringBuffer类独有的方法</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Vector-%E5%90%91%E9%87%8F-%E7%B1%BB"><span class="nav-number">4.</span> <span class="nav-text">Vector(向量)类</span></a></li></ol></div>
        </section>
        <!--/noindex-->

        <section class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">William Leung</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/murmur/archives">
          <span class="site-state-item-count">43</span>
          <span class="site-state-item-name">文章</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/murmur/categories/">
          
        <span class="site-state-item-count">9</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/murmur/tags/">
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </section>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">
      

      

  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/lianghuilin/murmur.git/university_notes/Java(5)-%E6%95%B0%E7%BB%84%E3%80%81%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%8E%E5%90%91%E9%87%8F/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/murmur/images/avatar.gif">
      <meta itemprop="name" content="William Leung">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="汇话板">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          计应本的自考笔记(5)-Java数组、字符串与向量
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-03-26 22:10:12" itemprop="dateCreated datePublished" datetime="2022-03-26T22:10:12+08:00">2022-03-26</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2022-03-27 16:48:08" itemprop="dateModified" datetime="2022-03-27T16:48:08+08:00">2022-03-27</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/murmur/categories/%E8%AE%A1%E5%BA%94%E6%9C%AC%E7%9A%84%E8%87%AA%E8%80%83%E7%AC%94%E8%AE%B0/" itemprop="url" rel="index"><span itemprop="name">计应本的自考笔记</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>3.7k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>3 分钟</span>
    </span>
</div>

            <div class="post-description">理解数组的概念，使用一维和多维数组；理解字符串概念，认识不可变的String类以及可变的StringBuffer类；理解向量的概念，掌握操作向量的相关方法；</div>
        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="一维数组"><a href="#一维数组" class="headerlink" title="一维数组"></a>一维数组</h2><ul>
<li>数组，是相同类型数据的有序集合。3个关键词，“相同类型的数据”+“有顺序的”+“集合”。实际上，系统一旦发现是数组集合，就会将集合中数据，存储在连续的内存位置上。</li>
<li>通过下标访问数组中的元素，下标从0开始。</li>
<li>数组是集合，存储的元素，可以是基础数据类型，也可以对象类型，或者是数组类型（也就是多维数组）。</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 定义基础数据类型数组</span></span><br><span class="line"><span class="keyword">int</span>[] numArr;   <span class="comment">// int类型的数组</span></span><br><span class="line"><span class="keyword">char</span> charArr[]; <span class="comment">// char类型的数组</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义对象类型的数组</span></span><br><span class="line">Date dateArr[]; <span class="comment">// Date类型的数组</span></span><br><span class="line">Count CountArr[];   <span class="comment">// Count类型的数组</span></span><br></pre></td></tr></table></figure>

<ul>
<li>数组需要先声明/定义再使用。就如对象一样，声明后要new进行实例化。而数组定义时，也不会创建数组对象本身，并为其分配内存。要经过初始化后，才能使用数组，包括访问数组的元素等。</li>
<li>但值得注意的是，Java不支持静态的数组定义，也就是声明数组时指定数组长度。如<code>int numArr[5];</code></li>
<li>数组的创建或者说初始化，分为“静态初始化”和“动态初始化”两种。<ul>
<li>静态初始化，就是在定义数组的同时，对数组的元素进行赋值。</li>
<li>而动态初始化，则是先通过new运算符，创建指定数据类型的数组，并指定数组的长度，后续通过下标来给数组元素赋值。</li>
</ul>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 数组的静态初始化</span></span><br><span class="line"><span class="keyword">int</span>[] numArr = &#123;<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;</span><br><span class="line"><span class="keyword">char</span> charArr[] = &#123;<span class="string">&#x27;C&#x27;</span>, <span class="string">&#x27;h&#x27;</span>, <span class="string">&#x27;i&#x27;</span>, <span class="string">&#x27;n&#x27;</span>, <span class="string">&#x27;a&#x27;</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 数组的动态初始化 Liang</span></span><br><span class="line">String names[];</span><br><span class="line">names = <span class="keyword">new</span> String[<span class="number">5</span>];</span><br><span class="line">names[<span class="number">0</span>] = <span class="string">&#x27;L&#x27;</span>;</span><br><span class="line">names[<span class="number">1</span>] = <span class="string">&#x27;i&#x27;</span>;</span><br><span class="line">names[<span class="number">2</span>] = <span class="string">&#x27;a&#x27;</span>;</span><br><span class="line">names[<span class="number">3</span>] = <span class="string">&#x27;n&#x27;</span>;</span><br><span class="line">names[<span class="number">4</span>] = <span class="string">&#x27;g&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 对象类型的动态初始化</span></span><br><span class="line">Date age[] = <span class="keyword">new</span> Dat[<span class="number">2</span>];</span><br><span class="line">age[<span class="number">0</span>] = <span class="keyword">new</span> Date(<span class="string">&#x27;1949&#x27;</span>);</span><br><span class="line">age[<span class="number">1</span>] = <span class="keyword">new</span> Date(<span class="string">&#x27;2022&#x27;</span>);</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ul>
<li>通过下标，访问数组内的元素，下标也称为索引，都是从0开始，可以是整数的常量或者表达式。</li>
<li>数组对象的length属性表示数组内元素的个数，我们可以通过length属性进行数组的边界检查。</li>
<li>实际上，我们使用new创建数组时，会指定数组的长度，这时系统会自动赋值给length属性。而一旦数组创建，大小就固定了。</li>
</ul>
<h2 id="多维数组"><a href="#多维数组" class="headerlink" title="多维数组"></a>多维数组</h2><ul>
<li>如果数组中元素也是数组，就是二维数组。同理，数组中再存数组，层层套娃，就是多维数组。</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span>[][] pointArr;   <span class="comment">// 定义二维的int数组</span></span><br><span class="line"><span class="keyword">char</span> strArr[][][];  <span class="comment">// 定义三维的char数组</span></span><br><span class="line">Date dateRangeArr[][][][];  <span class="comment">// 定义四维的Date数组</span></span><br></pre></td></tr></table></figure>

<ul>
<li>与一维数组一样，多维数组的初始化也分为“静态初始化”和“动态初始化”</li>
<li>静态初始化时，不必指定数组每一维的大小，系统会根据初始化给的初始值，自动计算数组每一维的大小</li>
<li>动态初始化时，有两种分配内存的方式：直接分配与按维度分配<ul>
<li>直接分配就是声明数组时，给出各个维度数组的大小；</li>
<li>按维分配就是从最高维度起，分别为每个维度分配内存；</li>
</ul>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 静态初始化</span></span><br><span class="line"><span class="keyword">int</span> intArr[][] = &#123;&#123;<span class="number">1</span>, <span class="number">2</span>&#125;, &#123;<span class="number">1</span>, <span class="number">3</span>, <span class="number">5</span>&#125;, &#123;<span class="number">2</span>, <span class="number">4</span>, <span class="number">6</span>&#125;&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 动态初始化-直接分配</span></span><br><span class="line"><span class="keyword">int</span> a[][] = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">2</span>][<span class="number">3</span>];</span><br><span class="line"></span><br><span class="line"><span class="comment">// 动态初始-按维分配</span></span><br><span class="line"><span class="keyword">int</span> twoDim[][] = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">4</span>][]; <span class="comment">// 先只分配第一维的数组大小</span></span><br><span class="line">twoDim[<span class="number">0</span>] = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">5</span>];</span><br><span class="line">twoDim[<span class="number">1</span>] = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">5</span>];</span><br><span class="line">twoDim[<span class="number">2</span>] = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">5</span>];</span><br><span class="line">twoDim[<span class="number">3</span>] = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">5</span>];</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ul>
<li>多维数组也有length属性，也通过下标来访问数组内的元素。</li>
<li>值得注意的是，不管一维还是多维数组，都是定长的。一旦创建后，数组的大小就不能再变化。如果需要改变数组，可以通过<code>System.arraycopy()</code>方法，将数组重新赋值给一个新的数组。</li>
<li>同时，<code>java.util.Arrays</code>类，提供了一系列数组的静态方法：<ul>
<li><code>equals(type[], type[])</code>：判断两个数组的值是否相同；</li>
<li><code>sort(type[])</code>：将数组按升序进行排序（如果数组内元素是对象，此方法将调用元素内对象的compareTo方法来比较）；</li>
<li><code>fill(type[], type value)</code>：将value值填充到数组的每个元素；</li>
<li><code>binarySearch(type[], type value)</code>：采用二分查找法在数组中查找value值；</li>
</ul>
</li>
</ul>
<h2 id="字符串类型"><a href="#字符串类型" class="headerlink" title="字符串类型"></a>字符串类型</h2><h3 id="String与StringBuffer"><a href="#String与StringBuffer" class="headerlink" title="String与StringBuffer"></a>String与StringBuffer</h3><ul>
<li>Java中的字符串是由数个字符组成的序列，同时也是一个对象。Java的标准包<code>java.lang</code>封装了String和StringBuffer类，分别处理不变字符串和可变字符串。</li>
<li>顾名思义，不变字符串是字符串一旦创建，内容就不能改变，即便调用String类的查找、比较、连接等操作，也不能插入新字符串，或者改变原来字符串本身。</li>
<li>Java字符串分为“常量”和“变量”，常量用双引号括起来，创建过程是隐式的，比如打印日志<code>System.out.println(&quot;This is String&quot;)</code>；而变量则要先声明后初始化；</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">String s1;</span><br><span class="line">StringBuffer sb1;</span><br><span class="line"></span><br><span class="line">s1 = <span class="keyword">new</span> String();</span><br><span class="line">sb1 = <span class="keyword">new</span> StringBuffer();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 由字符串数组，创建字符串</span></span><br><span class="line"><span class="keyword">char</span> chars = &#123;<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>&#125;;</span><br><span class="line">String s2 = <span class="keyword">new</span> String(chars);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 用字符串常量初始化</span></span><br><span class="line">String s3 = <span class="string">&quot;Hello World&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// StringBuffer需要new来创建</span></span><br><span class="line">StringBuffer strb3 = <span class="keyword">new</span> StringBuffer(s3);</span><br></pre></td></tr></table></figure>

<h3 id="字符串操作与判断"><a href="#字符串操作与判断" class="headerlink" title="字符串操作与判断"></a>字符串操作与判断</h3><ul>
<li>前面已经提到，String类的字符串是不可变的字符串。操作String字符串后，并不改变字符串本身，而是生成另一个实例。</li>
<li>StringBuffer类字符串处理可变字符串，不创建新字符串对象，而是直接操作字符串本身。</li>
</ul>
<h4 id="String和StringBuffer共有方法"><a href="#String和StringBuffer共有方法" class="headerlink" title="String和StringBuffer共有方法"></a>String和StringBuffer共有方法</h4><ul>
<li><code>length()</code>：返回字符串长度（字符个数）；</li>
<li><code>charAt(int index)</code>：返回字符串中指定索引位置index的字符；</li>
<li><code>subString(int beginIndex)</code>：从beginIndex位置开始截取字符串，直到末尾；</li>
</ul>
<h4 id="String类独有的方法"><a href="#String类独有的方法" class="headerlink" title="String类独有的方法"></a>String类独有的方法</h4><ul>
<li><code>replace(char oldChar, char newChar)</code>：将当前字符串中所有的oldChar，替换成newChar；</li>
<li><code>toLowerCase()</code>：将当前字符串中所有字符转换成小写形式；</li>
<li><code>toUpperCase()</code>：将当前字符串中所有字符转换成大写形式；</li>
<li><code>concat(String str)</code>：将str字符串，拼接到当前字符串末尾；</li>
<li><code>starsWith(String prefix)</code>：测试prefix是否为当前字符串的前缀；</li>
<li><code>trim()</code>：剔除字符串前后的空白；</li>
<li><code>valueOf(type value)</code>：将任意数据类型的参数value，转换成字符串；</li>
</ul>
<h4 id="StringBuffer类独有的方法"><a href="#StringBuffer类独有的方法" class="headerlink" title="StringBuffer类独有的方法"></a>StringBuffer类独有的方法</h4><ul>
<li><code>append(String str)</code>：将参数str添加到当前字符串的最后；</li>
<li><code>replace(int start, ind end, String str)</code>：将当前字符串中start到end的位置，替换成参数str；</li>
<li><code>capacity()</code>：返回当前字符串的容量；<code>strbuferr_length = str_num + capacity</code></li>
</ul>
<blockquote>
<p>注意：系统为String类对象分配内存时，是按字符的实际个数等量分配的；而StringBuffer类分配内存时，除去字符所占的空间外，会再另外加入16个字符大小的缓冲区。</p>
</blockquote>
<h2 id="Vector-向量-类"><a href="#Vector-向量-类" class="headerlink" title="Vector(向量)类"></a>Vector(向量)类</h2><ul>
<li>Java引入Vector(向量)类，可看作是一个可变数组。这个数组的大小根据需要来改变，增加或删除元素，Vector会相应的变大或变小。除此之外，Vector可以存储不同类型的元素，但不能是基础数据类型。</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Vector vList = <span class="keyword">new</span> Vector();</span><br><span class="line">vList.add(<span class="number">0</span>, <span class="keyword">new</span> String(<span class="string">&quot;开国大典&quot;</span>));</span><br><span class="line">vList.add(<span class="number">1</span>, <span class="keyword">new</span> Date(<span class="number">1949</span>));</span><br><span class="line">vList.add(<span class="number">2</span>, <span class="keyword">new</span> String(<span class="string">&quot;举办北京奥运会&quot;</span>));</span><br><span class="line">vList.add(<span class="number">3</span>, <span class="keyword">new</span> Date(<span class="number">2008</span>));</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>William Leung
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://gitee.com/lianghuilin/murmur.git/university_notes/Java(5)-%E6%95%B0%E7%BB%84%E3%80%81%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%8E%E5%90%91%E9%87%8F/" title="计应本的自考笔记(5)-Java数组、字符串与向量">https://gitee.com/lianghuilin/murmur.git/university_notes/Java(5)-数组、字符串与向量/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/murmur/university_notes/Java(4)-%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1/" rel="prev" title="计应本的自考笔记(4)-Java面向对象程序设计">
                  <i class="fa fa-chevron-left"></i> 计应本的自考笔记(4)-Java面向对象程序设计
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/murmur/university_notes/Java(6)-%E7%BB%A7%E6%89%BF%E4%B8%8E%E5%A4%9A%E6%80%81/" rel="next" title="计应本的自考笔记(6)-Java继承与多态">
                  计应本的自考笔记(6)-Java继承与多态 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






      

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

    </div>
  </main>

  <footer class="footer">
    <div class="footer-inner">
      

      

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">William Leung</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">142k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">2:09</span>
  </span>
</div>
<div class="busuanzi-count">
    <span id="busuanzi_container_site_uv">
      本站访客数<span id="busuanzi_value_site_uv"></span>人次
    </span>
    <span id="busuanzi_container_site_pv">
        本站总访问量<span id="busuanzi_value_site_pv"></span>次
    </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/murmur/js/utils.js"></script><script src="/murmur/js/motion.js"></script><script src="/murmur/js/next-boot.js"></script>

  
















  <script>
    NProgress.configure({
      showSpinner: true
    });
    NProgress.start();
    document.addEventListener('readystatechange', () => {
      if (document.readyState === 'interactive') {
        NProgress.inc(0.8);
      }
      if (document.readyState === 'complete') {
        NProgress.done();
      }
    });
    document.addEventListener('pjax:send', () => {
      NProgress.start();
    });
    document.addEventListener('pjax:success', () => {
      NProgress.done();
    });
  </script>


  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>








  

  

</body>
</html>
